package logic

import (
	"context"
	"fmt"

	"house_srv/house"
	"house_srv/internal/svc"
	"house_srv/models"
	"house_srv/pkg"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 实名验证
func (l *RealNameLogic) RealName(in *house.RealNameRequest) (*house.RealNameResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	name := models.RealName{
		UserId:   int(in.UserId),
		IdCard:   in.IdCard,
		RealName: in.RalName,
	}
	if err = name.RealNameList(); err != nil {
		logx.Error("用户实名信息错误")
		return nil, fmt.Errorf("用户信息错误")
	}
	if name.ID > 0 {
		logx.Error("用户实名信息不存在")
		return nil, fmt.Errorf("用户信息不存在")
	}
	if !pkg.RealName(in.IdCard, in.RalName) {
		logx.Error("第三方实名信息错误")
		return nil, fmt.Errorf("第三方实名信息错误")
	}
	if err = name.RealNameAdd(); err != nil {
		logx.Error("用户实名信息添加失败")
		return nil, fmt.Errorf("用户实名信息添加失败")
	}
	return &house.RealNameResponse{}, nil
}
